////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils
{

	/**
	   * 
	   * @param d
	   * @param seonds  -1: before 1 seconds, 1: after 1 seconds
	   * @return
	   */
	public static Date getRecentDateTime(Date d, int seconds)
	{
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.SECOND, now.get(Calendar.SECOND) + seconds);
		return now.getTime();
	}

	/**
	   * 
	   * @param d
	   * @param day
	   * @return
	   */
	public static Date getDateBefore(Date d, int day)
	{
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}

	/**
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateAfter(Date d, int day)
	{
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}

	public static String formatDate(Date date, String format)
	{
		SimpleDateFormat simpleDateformat = new SimpleDateFormat(format);
		return simpleDateformat.format(date);
	}

	public static Date strToDate(String date, String format)
	{
		Date result = null;
		SimpleDateFormat df = new SimpleDateFormat(format);
		try
		{
			result = df.parse(date);
		}
		catch (java.text.ParseException e1)
		{

		}

		return result;
	}

}
